package com.whmall.service.crm.provider;

import com.whmall.component.common.ObjectConverter;
import com.whmall.service.crm.api.CompanySupplierApi;
import com.whmall.service.crm.entity.dto.company.CompanyQueryRspDTO;
import com.whmall.service.crm.entity.dto.company.SupplierAccountReqDTO;
import com.whmall.service.crm.module.company.entity.SupplierAccountDO;
import com.whmall.service.crm.module.company.service.CompanyService;
import com.whmall.service.crm.module.company.service.CompanySupplierService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Author yangkuan
 * @Date 2020/8/4 14:30
 **/
@Service(interfaceClass = CompanySupplierApi.class)
public class CompanySupplierProvider implements CompanySupplierApi{

    @Autowired
    private final ObjectConverter<SupplierAccountDO, SupplierAccountReqDTO> supplierAccountReqDO2SupplierAccountReqDTO;

    public CompanySupplierProvider(ObjectConverter<SupplierAccountDO, SupplierAccountReqDTO>
                                           supplierAccountReqDO2SupplierAccountReqDTO) {
        this.supplierAccountReqDO2SupplierAccountReqDTO = supplierAccountReqDO2SupplierAccountReqDTO;
    }

    @Autowired
    private CompanyService companyService;

    @Autowired
    private CompanySupplierService companySupplierService;

  //  @Override
    public CompanyQueryRspDTO queryCompanySupplierById(Long companyId) {

        CompanyQueryRspDTO companyQueryRspDTO = companyService
                .queryCompanyById(companyId);
        List<SupplierAccountDO> supplierAccountDOS = companySupplierService
                .queryCompanySupplierById(companyId);
        List<SupplierAccountReqDTO> supplierAccountReqDTOS = null;
        if (supplierAccountDOS != null && supplierAccountDOS.size() > 0){
            supplierAccountReqDTOS = supplierAccountDOS.stream()
                    .map(supplierAccountReqDO2SupplierAccountReqDTO::convert)
                    .collect(Collectors.toList());
        }
        companyQueryRspDTO.setSupplierAccountRspDTOS(supplierAccountReqDTOS);
        return companyQueryRspDTO;
    }

    @Override
    @Transactional
    public void updateCompanySupplier(Long supplierId, SupplierAccountReqDTO supplierAccountReqDTO) {
        companySupplierService.updateCompanySupplier(supplierId, supplierAccountReqDTO);
    }

    @Override
    @Transactional
    public Long insertSupplierAccount(SupplierAccountReqDTO supplierAccountReqDTO) {
        return companySupplierService.insertSupplierAccount(supplierAccountReqDTO);
    }

    @Override
    public CompanyQueryRspDTO querySupplierInfoById(Long companyId) {
        CompanyQueryRspDTO companyQueryRspDTO1 = companyService
                .queryCompanyById(companyId);
        CompanyQueryRspDTO companyQueryRspDTO = new CompanyQueryRspDTO();

        companyQueryRspDTO.setId(companyQueryRspDTO1.getId());
        companyQueryRspDTO.setMaxProductCount(companyQueryRspDTO1.getMaxProductCount());
        companyQueryRspDTO.setNeedDetect(companyQueryRspDTO1.getNeedDetect());
        companyQueryRspDTO.setSupplierSettlementMethod(companyQueryRspDTO1.getSupplierSettlementMethod());
        companyQueryRspDTO.setIsCanMspoa(companyQueryRspDTO1.getIsCanMspoa());
        List<SupplierAccountDO> supplierAccountDOS = companySupplierService
                .queryCompanySupplierById(companyId);
        List<SupplierAccountReqDTO> supplierAccountReqDTOS = null;
        if (supplierAccountDOS != null && supplierAccountDOS.size() > 0){
            supplierAccountReqDTOS = supplierAccountDOS.stream()
                    .map(supplierAccountReqDO2SupplierAccountReqDTO::convert)
                    .collect(Collectors.toList());
        }
        companyQueryRspDTO.setSupplierAccountRspDTOS(supplierAccountReqDTOS);
        return companyQueryRspDTO;
    }


    @Override
    @Transactional
    public void setDefaultSupplier(Long supplierId, Long reqId) {
        companySupplierService.setDefaultSupplier(supplierId, reqId);
    }
}
